from datetime import datetime
from app import db

class Category(db.Model):
    """视频分类表"""
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50), nullable=False, unique=True)
    description = db.Column(db.String(200))
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    videos = db.relationship('Video', backref='category', lazy=True)

class Video(db.Model):
    """视频表"""
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(100), nullable=False)
    description = db.Column(db.Text)
    cover_url = db.Column(db.String(200))
    video_url = db.Column(db.String(200))
    duration = db.Column(db.Integer)  # 视频时长（秒）
    size = db.Column(db.Integer)      # 文件大小（字节）
    views = db.Column(db.Integer, default=0)  # 观看次数
    status = db.Column(db.String(20), default='published')  # draft, published, archived
    category_id = db.Column(db.Integer, db.ForeignKey('category.id'))
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)

class Tag(db.Model):
    """标签表"""
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50), nullable=False, unique=True)
    videos = db.relationship('Video', secondary='video_tags', backref='tags')

# 视频-标签关联表
video_tags = db.Table('video_tags',
    db.Column('video_id', db.Integer, db.ForeignKey('video.id'), primary_key=True),
    db.Column('tag_id', db.Integer, db.ForeignKey('tag.id'), primary_key=True)
) 